/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：KeyboardEditPolicy.js<br>
 * 日期：2016年9月29日<br>
 * 作者: 王志远<br>
 * 功能：键盘编辑策略<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.KeyboardEditPolicy<br>
 * 类型功能：抽象模型<br>
 */
Rainier.UI.V2.KeyboardEditPolicy = draw2d.policy.canvas.ExtendedKeyboardPolicy.extend({
	
	NAME : "Rainier.UI.V2.KeyboardEditPolicy",
	
	/**
	 * 函数名称：Rainier.UI.V2.KeyboardEditPolicy.init<br>
	 * 函数功能：构造函数<br>
	 * 参数输入:property属性对象<br>
	 * 异常信息：无<br>
	 */
	init: function(){
		this._super();
		this.selected = new Object();
	},
	
	/**
	 * @method
	 * 
	 * @param {draw2d.Canvas} canvas
	 * @param {Number} x the x-coordinate of the mouse down event
	 * @param {Number} y the y-coordinate of the mouse down event
	 * @param {Boolean} shiftKey true if the shift key has been pressed during this event
	 * @param {Boolean} ctrlKey true if the ctrl key has been pressed during the event
	 */
	onMouseDown: function(canvas, x, y, shiftKey, ctrlKey){
		this._super(canvas, x, y, shiftKey, ctrlKey);
		var ctx = canvas.context;
		var rootEditPart = ctx.getRootEditPart();
		if(rootEditPart.getCreateState()){
			var createInfo = rootEditPart.getCreateInfo();
			var cmd = new Rainier.UI.V2.CommandAdd(x,y,createInfo,rootEditPart);
			canvas.getCommandStack().execute(cmd);
			rootEditPart.exitCreateState();
		}
		// window.ctx.updateMenuBtnsState();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.KeyboardEditPolicy._selectAll<br>
	 * 函数功能：选择所有图元<br>
	 * 参数输入：canvas:Rainier.UI.V2.Canvas<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	_selectAll: function(canvas){
		var ctx = canvas.context;
		var rootEditPart = ctx.getRootEditPart();
		Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){
			if(part instanceof Rainier.UI.V2.CompositeEditPart){
				var figure		= part.getFigure();
				if(figure instanceof draw2d.Canvas){
					return;
				}
				canvas.addSelection(figure);
//    			var connparts 	= part.getConnectionParts();
//    			connparts.each(function(i,c){
//    				var connfigure = c.getFigure();
//    				canvas.addSelection(connfigure);
//    			});
			}
		});
	},
	
	/**
	 * @method
	 * 
	 * @param {draw2d.Canvas} canvas
	 * @param {Number} x the x-coordinate of the mouse down event
	 * @param {Number} y the y-coordinate of the mouse down event
	 * @param {Boolean} shiftKey true if the shift key has been pressed during this event
	 * @param {Boolean} ctrlKey true if the ctrl key has been pressed during the event
	 */
	onKeyDown:function(canvas, keyCode, shiftKey, ctrlKey, event){
		
		var cmd = null;
		var context = canvas.context;
		var rootEditPart = context.getRootEditPart();
		
		if(keyCode === 27){//Esc//退出创建状态
			this._super(canvas, keyCode, shiftKey, ctrlKey);
			rootEditPart.exitCreateState();
			if(!rootEditPart.isCreateState){				
				rootEditPart.getFigure().html.css({"cursor":"default"});
			}
			return;
		}

		if(keyCode === 46){//Delete//删除器件更新预览图
			//更新预览图
			// if(isSelectedPreview){
			// 	setTimeout(function(){
			// 		var img = $("#img-preview");
			// 		refreshPreview(img);
			// 	});
			// }
		}

		if(keyCode === 9){//Tab键添加主题
			var selected = context.getSelected();
			if(selected.data.length === 0){
				var editPartArr = this.editPart.m_aChildren;
				for(var editPartId in editPartArr){
					var editPart = context.findEditPartById(editPartId);
					if(editPart.model.property.active == true){
						editPart.active();
					}
				}
			}
			var editParts = new draw2d.util.ArrayList();
			selected.clone().each(function(j,select){
				var editPart = context.findEditPartByModel(select);
				if(editPart){
					editParts.add(editPart);
				}
			})
			if(selected){
				if(selected.data.length==1){
					var typeName = selected.data[0].property.modelProperty.typeName;
					if(typeName === "TextModelJson" || typeName === "TextModelOval" || typeName === "TextModelDiamond"){
						cmd = new Rainier.UI.V2.CommandAddTheme(context,editParts);
						canvas.getCommandStack().execute(cmd);
						this._stopEventPropagation(event);
					}
					else if(typeName === "TextModelJsonPara"){
						cmd = new Rainier.UI.V2.CommandAddParaTheme(context,editParts);
						canvas.getCommandStack().execute(cmd);
						this._stopEventPropagation(event);
					}
				}
			}
		}
		
		if(ctrlKey ===true){
			switch(keyCode){
			case 65: //Ctrl+A //选中所有
				{
					this._selectAll(canvas);
					this._stopEventPropagation(event);
					break;
				}
			case 68 : //Ctrl+D //显示或取消辅助线 
				{
					console.log("display help line");
					this._stopEventPropagation(event);
					//在此用代码实现
					break;
				}
			case 88 : //Ctrl+X //剪切
				{
					var selected = context.getSelected();
					var editParts = new draw2d.util.ArrayList();
					selected.clone().each(function(j,select){
						var editPart = context.findEditPartByModel(select);
						if(editPart){
							editParts.add(editPart);
						}
					})
					cmd = new Rainier.UI.V2.CommandCut(context,editParts);
					canvas.getCommandStack().execute(cmd);
					this._stopEventPropagation(event);
					//更新预览图
					// if(isSelectedPreview){
					// 	setTimeout(function(){
					// 		var img = $("#img-preview");
					// 		refreshPreview(img);
					// 	}); 
					// }
					break;
				}
			case 67 : //Ctrl+C //复制
				{
					var selected = context.getSelected();
					var editParts = new draw2d.util.ArrayList();
					selected.clone().each(function(j,select){
						var editPart = context.findEditPartByModel(select);
						if(editPart){
							editParts.add(editPart);
						}
					})
					cmd = new Rainier.UI.V2.CommandCopy(context,editParts);
					canvas.getCommandStack().execute(cmd);
					this._stopEventPropagation(event);
					break;
				}
			case 86 : //Ctrl+V //粘贴
				{
					cmd = new Rainier.UI.V2.CommandPaste(context);
					canvas.getCommandStack().execute(cmd);
					this._stopEventPropagation(event);
					//更新预览图
					// if(isSelectedPreview){
					// 	setTimeout(function(){
					// 		var img = $("#img-preview");
					// 		refreshPreview(img);
					// 	}); 
					// }
					break;
				}
			case 90 : //Ctrl+Z //undo
				{
					//在此用代码实现
					canvas.getCommandStack().undo();
					this._stopEventPropagation(event);
					//更新预览图
					// if(isSelectedPreview){
					// 	setTimeout(function(){
					// 		var img = $("#img-preview");
					// 		refreshPreview(img);
					// 	}); 
					// }
					break;
				}
			case 89 : //Ctrl+Y //redo
				{
					//在此用代码实现
					canvas.getCommandStack().redo();
					this._stopEventPropagation(event);
					//更新预览图
					// if(isSelectedPreview){
					// 	setTimeout(function(){
					// 		var img = $("#img-preview");
					// 		refreshPreview(img);
					// 	}); 
					// }
					break;
				}
			case 84 : //Ctrl+T //垂直翻转
				{
					cmd = new Rainier.UI.V2.CommandVerticalFlip();
					canvas.getCommandStack().execute(cmd);
					this._stopEventPropagation(event);
					break;
				}
			case 72 : //Ctrl+H //水平翻转
				{
					cmd = new Rainier.UI.V2.CommandHorizontalFlip();
					canvas.getCommandStack().execute(cmd);
					this._stopEventPropagation(event);
					break;
				}
			case 82 : //Ctrl+R //顺时针旋转90度
				{
					var selected = context.getSelected();
					var editParts = new draw2d.util.ArrayList();
					selected.clone().each(function(j,select){
						var editPart = context.findEditPartByModel(select);
						if(editPart){
							editParts.add(editPart);
						}
					})
					if(editParts.getSize() === 1){
						if(shiftKey){
							cmd = new Rainier.UI.V2.CommandCounterClockwise(context,editParts.get(0));
						}
						else{
							cmd = new Rainier.UI.V2.CommandClockwise(context,editParts.get(0));
						}
						canvas.getCommandStack().execute(cmd);
					}
					this._stopEventPropagation(event);
					break;
				}
			 }
		}else{
		   this._super(canvas, keyCode, shiftKey, ctrlKey);
		}
		// window.ctx.updateMenuBtnsState();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.KeyboardEditPolicy._stopEventPropagation<br>
	 * 函数功能：组织浏览器事件，兼容ie和火狐等浏览器<br>
	 * 参数输入：触发事件源event<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	_stopEventPropagation : function(e){
		if(e.preventDefault){
			e.preventDefault();
			e.stopPropagation()
		}else{
			e.returnValue=false;
			e.cancelBubble=true
		}
	}
});
